class UserSessionsController < ApplicationController
  before_filter [:init_custom_includes, :init_trace_nav]
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Successfuly logged in."
      redirect_back_or_default(:controller => 'entries')
    else
      render :action => 'new'
    end
  end

  def destroy
    @user_session = UserSession.find
    @user_session.destroy
    flash[:notice] = "Successfully logged out."
    redirect_to root_url
  end

  def redirect_back_or_default(default)
    if session[:return_to]
      redirect_to session[:return_to]
    else
      redirect_to default
    end
    session[:return_to] = nil
  end
end
